package com.bishe.demo.modules.hotel.hotelFacilityAndService.service.impl;

import com.bishe.demo.modules.account.dao.UserDao;
import com.bishe.demo.modules.account.entity.User;
import com.bishe.demo.modules.common.entity.ResultEntity;
import com.bishe.demo.modules.hotel.common.dao.HotelImageDao;
import com.bishe.demo.modules.hotel.common.entity.HotelImage;
import com.bishe.demo.modules.hotel.hotelFacilityAndService.dao.*;
import com.bishe.demo.modules.hotel.hotelFacilityAndService.entity.*;
import com.bishe.demo.modules.hotel.hotelFacilityAndService.service.FacilityAndServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

@Service
public class FacilityAndServiceServiceImpl implements FacilityAndServiceService {
    @Autowired
    private FacilityDao facilityDao;

    @Autowired
    private HotelImageDao hotelImageDao;

    @Autowired
    private FacilityHotelImageDao facilityHotelImageDao;

    @Autowired
    private HServiceDao hServiceDao;

    @Autowired
    private HServiceUserDao hServiceUserDao;

    @Autowired
    private FacilityUserDao facilityUserDao;

    @Autowired
    private UserDao userDao;

    @Override
    @Transactional
    public ResultEntity<FacilityAndService> insertFacilityAndService(FacilityAndService facilityAndService) {

        List<Facility> facilities = facilityAndService.getFacilities();
        for (Facility facility : facilities) {
            facilityDao.insertFacility(facility);
            facilityUserDao.insertFacilityUser(new FacilityUser(facility.getFacilityId(), facilityAndService.getUser().getUserId()));
            List<HotelImage> facilityImages = facility.getFacilityImages();
            for (HotelImage hotelImage : facilityImages) {
                hotelImageDao.insertImage(hotelImage);
                facilityHotelImageDao.insertFacilityHotelImage(new FacilityHotelImage(facility.getFacilityId(), hotelImage.getId()));
            }
        }

        List<HService> services = facilityAndService.getServices();
        for (HService service : services) {
            hServiceDao.insertHService(service);
            hServiceUserDao.insertHServiceUser(new HServiceUser(service.gethServiceId(), facilityAndService.getUser().getUserId()));
        }
        return new ResultEntity<FacilityAndService>(ResultEntity.ResultStatus.SUCCESS.code, "添加成功", facilityAndService);
    }

    @Override
    public FacilityAndService getFacilityAndServiceByHotelId(int hotelId) {
        FacilityAndService facilityAndService = new FacilityAndService();
        List<Facility> facilitiesByHotelId = facilityDao.getFacilitiesByHotelId(hotelId);
        facilityAndService.setFacilities(facilitiesByHotelId);
        List<HService> hServicesByHotelId = hServiceDao.getHServicesByHotelId(hotelId);
        facilityAndService.setServices(hServicesByHotelId);
        User userByUserId = userDao.getUserByUserId(hotelId);
        facilityAndService.setUser(userByUserId);
        return facilityAndService;
    }

    @Override
    @Transactional
    public void deleteByHotelId(int userId) {
        List<Facility> facilitiesByHotelId = facilityDao.getFacilitiesByHotelId(userId);
        for (Facility facility : facilitiesByHotelId) {
            facilityDao.deleteById(facility.getFacilityId());
            List<HotelImage> facilityImages = facility.getFacilityImages();
            for (HotelImage facilityImage : facilityImages) {
                hotelImageDao.deleteImageByImageId(facilityImage.getId());
                facilityHotelImageDao.deleteFacilityImageByImageId(facilityImage.getId());
            }
        }
        facilityUserDao.deleteByUserId(userId);

        List<HService> hServicesByHotelId = hServiceDao.getHServicesByHotelId(userId);
        for (HService hService : hServicesByHotelId) {
            hServiceDao.deleteById(hService.gethServiceId());
            hServiceUserDao.deleteByhServiceId(hService.gethServiceId());
        }
    }
}
